package quq.missq.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.utils.VolleyTool;


public class ResetPasswordActivity1 extends BaseActivity implements View.OnClickListener, VolleyTool.HTTPListener {

    private RelativeLayout mGetCode;
    private ImageView mGetCodeIcon;
    private TextView mTime;
    private EditText mPhone;
    private EditText mCode;
    private int time = 60;
    private TextView mNextStep;
    private ImageView mBack;
    private String phone,code = "";
    private ProgressDialog dialog;
    private static final int RESET_PASSWORD = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reset_password_activity1);
        initView();
        initListener();

    }

    private void initView() {
        mPhone = (EditText)findViewById(R.id.phone);
        mCode = (EditText)findViewById(R.id.checkCode);
        mGetCode = (RelativeLayout)findViewById(R.id.getCode);
        mGetCodeIcon = (ImageView)findViewById(R.id.getCodeIcon);
        mTime = (TextView)findViewById(R.id.time);
        mNextStep = (TextView)findViewById(R.id.nextStep);
        mBack = (ImageView)findViewById(R.id.back);
    }

    private void initListener() {
        mGetCode.setOnClickListener(this);
        mNextStep.setOnClickListener(this);
        mBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.nextStep:
                verify();
                break;
            case R.id.back:
                this.finish();
                break;
            case R.id.getCode:
                getCode();
                break;
        }
    }

    private void getCode(){
        phone = mPhone.getText().toString();
        if (phone.isEmpty() || phone.length() != 11) {
            showToast("请输入正确的手机号");
            return;
        }
        dialog = ProgressDialog.show(this, "提示", "发送中。。。", false, false);
        Map<String,String> params = new HashMap<>();
        params.put("mobile",phone);
        params.put("type","1"); // 0 register 1 find password back
        VolleyTool.get(this,Constants.GET_CODE_URL, params, this, Constants.GET_CODE, null);
    }

    private void verify(){
        code = mCode.getText().toString();
        if(TextUtils.isEmpty(code)){
            showToast("请输入验证码");
            return;
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("code", code);
        params.put("type", "1");
        dialog = ProgressDialog.show(this, "提示", "验证中。。。", false, false);
        VolleyTool.post(this,Constants.VERIFY_CODE_URL, params, this, Constants.VERIFY_CODE, null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK && requestCode == RESET_PASSWORD){
            Intent intent = new Intent();
            intent.setClass(this,LoginActivity.class);
            startActivity(intent);
            this.finish();
        }
    }

    private Timer timer;

    @Override
    public <T> void onResponse(T t, int requestCode) {
        switch (requestCode) {
            case Constants.GET_CODE:
                try {
                    JSONObject obj = new JSONObject(t.toString());
                    if(obj.getInt("code") >= 0){
                        mGetCodeIcon.setVisibility(View.GONE);
                        mTime.setVisibility(View.VISIBLE);
                        timer = new Timer();
                        //设置自动播放时间
                        timer.schedule(new MyTimerTask(), 1000,1000);
                        showToast("验证码发送成功，请注意查收");
                    }else{
                        showToast(obj.getString("message"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                break;
            case Constants.VERIFY_CODE:
                verifyCode(t.toString());
                break;
            case R.id.back:
                this.finish();
                break;
        }
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    private void verifyCode(String str){
        try {
            JSONObject obj = new JSONObject(str);
            int code = obj.getInt("code");
            String message = obj.getString("message");
            String data=obj.getString("data");
            if(code >= 0){
                Intent intent = new Intent();
                intent.putExtra("phone",phone);
                intent.putExtra("code",data);
                intent.setClass(this,ResetPaswordActivity2.class);
                startActivityForResult(intent, RESET_PASSWORD);
            }else{
                showToast(message);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }

    }

    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    time--;
                    if (time >= 0) {
                        mTime.setText(time+"");
                    } else {
                        time = 60;
                        mGetCodeIcon.setVisibility(View.VISIBLE);
                        mTime.setVisibility(View.GONE);
                        timer.cancel();
                    }
                }
            });
        }
    }

}
